<template>
  <div class="page-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入门店名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="订单状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <div class="mb20">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card>
            <div class="card-item">
              <i class="el-icon-document"></i>
              <div class="card-content">
                <div class="card-value">125</div>
                <div class="card-label">今日订单</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card>
            <div class="card-item">
              <i class="el-icon-money"></i>
              <div class="card-content">
                <div class="card-value">¥6,542</div>
                <div class="card-label">今日收入</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card>
            <div class="card-item">
              <i class="el-icon-time"></i>
              <div class="card-content">
                <div class="card-value">18</div>
                <div class="card-label">待处理订单</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card>
            <div class="card-item">
              <i class="el-icon-pie-chart"></i>
              <div class="card-content">
                <div class="card-value">89.5%</div>
                <div class="card-label">订单完成率</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['servicess:orders:remove']"
          >批量删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:user:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column
        label="订单编号"
        align="center"
        key="orderNo"
        prop="orderNo"
        width="200"
        v-if="columns[0].visible"
      />
      <el-table-column
        label="用户名称"
        align="center"
        key="userName"
        prop="userName"
        v-if="columns[1].visible"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="服务项目"
        align="center"
        key="services"
        prop="services.serviceName"
        v-if="columns[2].visible"
        width="120"
      />
      <el-table-column
        label="金额"
        align="center"
        key="actualAmount"
        prop="actualAmount"
        v-if="columns[3].visible"
      />
      <el-table-column
        label="门店"
        align="center"
        key="store"
        prop="store.storeName"
        v-if="columns[4].visible"
      />
      <el-table-column
        label="订单状态"
        align="center"
        key="status"
        v-if="columns[5].visible"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.order_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createdAt"
        v-if="columns[6].visible"
        width="160"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="180"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['servicess:orders:query']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['servicess:orders:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['servicess:orders:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listOrder,
  delOrder,
  updateOrder,
  getOrder,
} from "@/api/serve/orders";

export default {
  name: "Orders",
  dicts: ["order_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单列表数据
      orderList: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: undefined,
        userName: undefined,
        storeName: undefined,
        status: undefined,
      },
      // 列信息
      columns: [
        { key: 0, label: `订单编号`, visible: true },
        { key: 1, label: `用户名称`, visible: true },
        { key: 2, label: `服务项目`, visible: true },
        { key: 3, label: `金额`, visible: true },
        { key: 4, label: `门店`, visible: true },
        { key: 5, label: `订单状态`, visible: true },
        { key: 6, label: `创建时间`, visible: true },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      const params = {
        ...this.queryParams,
        beginTime: this.dateRange[0],
        endTime: this.dateRange[1],
      };
      listOrder(params)
        .then((response) => {
          console.log("订单列表响应数据：", response);
          if (response.code === 200) {
            this.orderList = response.rows;
            this.total = response.total;
          } else {
            this.$modal.msgError(response.msg || "获取订单列表失败");
          }
        })
        .catch((error) => {
          console.error("获取订单列表出错：", error);
          this.$modal.msgError("获取订单列表失败");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.orderId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal
        .confirm('是否确认删除订单编号为"' + orderIds + '"的数据项？')
        .then(function () {
          return delOrder(orderIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 查看按钮操作 */
    handleView(row) {
      const orderId = row.orderId;
      this.$router.push({ path: "/servicess/orders/view/" + orderId });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/user/export",
        {
          ...this.queryParams,
        },
        `user_${new Date().getTime()}.xlsx`
      );
    },
    /** 日期变化处理 */
    handleDateChange(val) {
      this.dateRange = val;
      this.handleQuery();
    },
  },
};
</script>

<style>
.card-item {
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-item i {
  font-size: 40px;
  margin-right: 15px;
}

.card-content {
  display: flex;
  flex-direction: column;
}

.card-value {
  font-size: 24px;
  font-weight: bold;
}

.card-label {
  font-size: 14px;
  color: #606266;
}

.page-container {
  margin-top: 20px;
  padding: 0 20px;
}

.mb20 {
  margin-bottom: 20px;
}
</style>
